package com.nutrition.health.repository;

import com.nutrition.health.model.KGNode;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface KGNodeRepository extends JpaRepository<KGNode, Long> {

    KGNode findByNameAndType(String name, String type);

    List<KGNode> findByType(String type);

    @Query("SELECT n FROM KGNode n JOIN KGRelation r ON n.id = r.targetId WHERE r.sourceId = :foodId AND r.relationType = '包含营养素'")
    List<KGNode> findNutrientsByFoodId(@Param("foodId") Long foodId);

    @Query("SELECT n FROM KGNode n JOIN KGRelation r ON n.id = r.targetId WHERE r.sourceId = :foodId AND r.relationType = '营养素影响功效'")
    List<KGNode> findBenefitsByFoodId(@Param("foodId") Long foodId);

    @Query("SELECT n FROM KGNode n WHERE n.type = '食物' AND n.name LIKE %:keyword%")
    List<KGNode> findFoodsByKeyword(@Param("keyword") String keyword);
}